package com.ftg.learn.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.ftg.learn.service.IDemo;
import com.ftg.learn.util.JsonWeb;
import com.ftg.learn.util.JsonWebUtil;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.List;

/**
 * 控制器
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/5/23 上午8:31
 */
@RestController
@RequestMapping("/api/demo")
public class DemoController {

    @Resource
    IDemo iDemo;

    @GetMapping("/hello")
    @SentinelResource(value = "h1", blockHandler = "exceptionHandler", fallback = "helloFallback")
    public JsonWeb<String> hello(){
        return JsonWebUtil.success(iDemo.remoteHello());
    }

    /**
     * synchronized 对象锁
     *synchronized 块锁 方法锁
     * @param s
     * @return
     */
    public  String  helloFallback(long s) {
        synchronized(new Long(s)){
            return String.format("请求数 %d", s);
        }
    }

    public String exceptionHandler(long s, BlockException ex) {
        ex.printStackTrace();
        return "对不起！当前访问人数过多" + s;
    }

    @GetMapping("/feignhello")
    public JsonWeb<String> feignHello(){
        return JsonWebUtil.success(iDemo.feignHello());
    }

}
